#!/usr/bin/env python

import itertools
import os
import sys

# Recompile all sources
os.chdir('src')
os.system('rm *.class')
os.system('javac Main.java')

input_folder = '../input'
output_folder = '../output'

# Create input/output folders.
if not os.path.exists(input_folder):
    os.mkdir(input_folder)
if not os.path.exists(output_folder):
    os.mkdir(output_folder)

# Combinations of initial values
incomes = ['0', '100', '10000']
checkings = ['0', '10000', '200']
savings = ['0', '10000', '200']
debt = ['0', '10', '200']
expenses = ['0', '100', '10000']
rates = ['0.1', '6.7', '23.5', '102' ]
test = 1
iterations = 100

# Iterate through the combinations
for r in itertools.product(incomes, checkings, savings, debt, expenses, rates, rates):

    # if expense is  greater than savings and checkings combined
    if float(r[4]) > ( float(r[1]) + float(r[2]) ) :
        continue

    # if debt is greater than savings and checkings combined
    if float(r[3]) != 0.0 and float(r[3]) > ( float(r[1]) + float(r[2]) ):
        continue

    # if expenses is greater than income
    if float(r[4]) > float(r[0]) :
        continue

    # if interest is outrageous
    if float(r[5]) > 100.0 or float(r[6]) > 100.0:
        continue

    # Set input/output file name
    infile = '%s/%d.csv' % (input_folder, test)
    outfile = '%s/%d.csv' % (output_folder, test)

    # Assign values for output
    initial_values = list(r)
    initial_values.insert(0, '0')
    initial_values.append('initial')

    # Write initial values to input file
    f = open(infile, 'w')
    f.write(';'.join(initial_values))
    f.close()

    os.system('echo.')
    os.system('echo iteration %d:' % test)
    os.system('java Main %s %s %d' % (infile, outfile, iterations))
    test += 1
